<?php
/**
*File Name:newfile.php
*Date:2016年3月1日上午11:45:25
*Copyright  (c)2016,783188184@qq.com All Rights Reserved
*____________________________________________________________________________________________________
*/
namespace Admin\Controller;
use Think\Controller;
class MoviemanageController extends Controller{
	//添加电影呈现
	public function movieadd(){
		$m = M('vterm');
		$res = $m->order(array("id"=>"asc"))->select();
		foreach ($res as $value){
			$a['id'] = $value['id'];
			$a['parentid'] = $value['p_id'];
			$a['name'] = $value['term_name'];
			$a['ename'] = $value['term_ename'];
			$c[] = $a;
		}
		$addtree = new \Org\Util\Tree();
		$addttree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$addtree->nbsp  = '&nbsp;&nbsp;&nbsp;';
		$addtree->init($c);
		$addstr = "<option value='\$id'>\$spacer\$name</option>";
		$addlist = $addtree->get_tree(0, $addstr);
		$this->assign('termlist',$addlist);
		$this->display();
	}
	
	//电影post数据接收
	public function add_post(){
		if(IS_POST){
			if(empty($_POST['v_pid'])){
				$this->error('请选择所属分类');
			}
			//判断第一路径是否有问题
			$path1 = splitpath($_POST['downpath1_1'],$_POST['downpath1_2'],$_POST['v_downterm1']);
            if($path1){
            	$_POST['v_downpath1'] = $path1;
            }
            
            //判断第二路径是否有问题
            $path2 = splitpath($_POST['downpath2_1'],$_POST['downpath2_2'],$_POST['v_downterm2']);
            if($path2){
            	$_POST['v_downpath2'] = $path2;
            }
            //判断第三路径是否有问题
            $path3 = splitpath($_POST['downpath3_1'],$_POST['downpath3_2'],$_POST['v_downterm3']);
            if($path3){
            	$_POST['v_downpath3'] = $path3;
            }
			$m = M('movie');
            $info = $_POST;
            $_POST['v_addtime'] = time();
            $res = $m->add($_POST);  
			if($res){
				$this->success('添加成功');
				$_SESSION['now_movieimg'] = null;
			}else{
				$this->error('添加失败');
			}
		}else{
			$this->error('请以正确的方式提交');
		}
		
	}
	//电影海报ajax上传 
	public function ajax_img(){
		if(IS_POST){
			if(!empty($_SESSION['now_movieimg'])){
				unlink("./Uploads/movie/".$_SESSION['now_movieimg']);
			}
			$savepath = date('Ymd').'/';
			$config = array(
					'rootPath' => MU_PATH.'/',
					'savePath' => $savepath,
            		'maxSize' => 11048576,
            		'saveName'   =>    array('uniqid',''),
            		'exts'       =>    array('jpg', 'gif', 'png', 'jpeg'),
            		'autoSub'    =>    false,
			);
			$upload = new \Think\Upload($config);
			$info=$upload->upload();
			//开始上传文件
		if ($info) {
                //上传成功
                //传出图片信息 
                $first=array_shift($info);
                if(!empty($first['url'])){
                	$url=$first['url'];
                }else{
                	$url=C("TMPL_PARSE_STRING.__UPLOAD__").$savepath.$first['savename'];
                }
                //为了防止上传多张海报而不把原来的删除
                $_SESSION['now_movieimg'] = $savepath.$first['savename'];
				echo "1," . $url.",".'1,'.$first['name'];
				exit;
            } else {
                //上传失败，返回错误
                exit("0," . $upload->getError());
            }
		}
	}
	//电影列表
	public function movielist(){
        $movie = M('movie');
        $count = $movie->count();
        $Page = new \Think\Page($count,20);//每页显示的条数
        //分页样式
        $Page -> setConfig('header','共%TOTAL_ROW%条');
        $Page -> setConfig('first','首页');
        $Page -> setConfig('last','共%TOTAL_PAGE%页');
        $Page -> setConfig('prev','上一页');
        $Page -> setConfig('next','下一页');
        $Page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
        $Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $Page->show();// 分页显示输出
        $list = $movie->limit($Page->firstRow.','.$Page->listRows)->select();
        $vterm = M('vterm');
        $vterminfo = $vterm->select();
        foreach ($vterminfo as $value){
        	$a['id'] = $value['id'];
        	$a['parentid'] = $value['p_id'];
        	$a['name'] = $value['term_name'];
        	$a['ename'] = $value['term_ename'];
        	$c[] = $a;
        }
        $tree = new \Org\Util\Tree();
        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
        $tree->nbsp  = '&nbsp;&nbsp;&nbsp;';
        $tree->init($c);
        $treestr = "<option value='\$id'>\$spacer\$name</option>";
        $treelist = $tree->get_tree(0, $treestr);

        $this->assign('treelist',$treelist);
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('vterminfo',$vterminfo);
		$this->display();
		
	}
	//列表页分类查找功能
	public function movie_list_search(){
		$where = array_filter($_POST);
		if(!empty($where['v_title'])){
			$where['v_title'] = array('like','%'.$where['v_title'].'%');
		}
		$movie = M('movie');
		$count = $movie->where($where)->count();
		$Page = new \Think\Page($count,20);//每页显示的条数
		//分页样式
		$Page -> setConfig('header','共%TOTAL_ROW%条');
		$Page -> setConfig('first','首页');
		$Page -> setConfig('last','共%TOTAL_PAGE%页');
		$Page -> setConfig('prev','上一页');
		$Page -> setConfig('next','下一页');
		$Page -> setConfig('link','indexpagenumb');//pagenumb 会替换成页码
		$Page -> setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
		$show = $Page->show();// 分页显示输出
		$list = $movie->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
        //树的生成
		$vterm = M('vterm');
		$vterminfo = $vterm->select();
		foreach ($vterminfo as $value){
			$a['id'] = $value['id'];
			$a['parentid'] = $value['p_id'];
			$a['name'] = $value['term_name'];
			$a['selected'] = $where['v_pid']==$a['id'] ? "selected":'';
			$c[] = $a;
		}
		$tree = new \Org\Util\Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp  = '&nbsp;&nbsp;&nbsp;';
		$tree->init($c);
		$treestr = "<option value='\$id' \$selected>\$spacer\$name</option>";
		$treelist = $tree->get_tree(0, $treestr);
		//数生成结束
		$this->assign('treelist',$treelist);
		$this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
		$this->assign('vterminfo',$vterminfo);
		$this->assign('area',$where['v_area']);
		$this->display();		
	}
	
	//批量移动
	public function movie_lotmove(){
		$v_id = $_POST['v_id'];
		$where['v_id'] = array('in',$v_id);
		$movie = M('movie');
		$movie->v_pid = $_POST['v_pid'];
		$movie->v_area = $_POST['v_area'];
			$res = $movie->where($where)->save();
			if(!$res){
				$this->error('修改失败');
			}else{
				$this->success('批量修改成功');
			}
	}
	//电影编辑
	public function movie_edit(){
		$id = $_GET['id'];
		$movie = M('movie');
		$movieinfo = $movie->where('v_id='.$id)->find();
		//将所属不同分类的下载地址分开以$$$
		if(!is_null($movieinfo['v_downpath1'])){
			$arr1 = explodepath($movieinfo['v_downpath1'],1);
		}
		if(!is_null($movieinfo['v_downpath2'])){
			$arr2 = explodepath($movieinfo['v_downpath2'],2);
		}
		if(!is_null($movieinfo['v_downpath3'])){
			$arr3 = explodepath($movieinfo['v_downpath3'],3);
		}
		//生成分类树
		$vterm = M('vterm');
		$vterminfo = $vterm->select();
		foreach ($vterminfo as $value){
			$a['id'] = $value['id'];
			$a['parentid'] = $value['p_id'];
			$a['name'] = $value['term_name'];
			$a['ename'] = $value['term_ename'];
			$a['selected'] = $movieinfo['v_pid']==$a['id'] ? 'selected':'';
			$c[] = $a;
		}
		$tree = new \Org\Util\Tree();
		$tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ');
		$tree->nbsp  = '&nbsp;&nbsp;&nbsp;';
		$tree->init($c);
		$treestr = "<option value='\$id' \$selected>\$spacer\$name</option>";
		$treelist = $tree->get_tree(0, $treestr);
		//结束分类树
		$this->assign($arr1);
		$this->assign($arr2);
		$this->assign($arr3);
		$this->assign($movieinfo);
		$this->assign('treelist',$treelist);
		$this->display();
	}
	
	//电影编辑后post
	public function movie_edit_post(){
		if(IS_POST){
			if(empty($_POST['v_pid'])){
				$this->error('请选择所属分类');
			}
			//判断第一路径是否有问题
			$path1 = splitpath($_POST['downpath1_1'],$_POST['downpath1_2'],$_POST['v_downterm1']);
			if($path1){
				$_POST['v_downpath1'] = $path1;
			}
		
			//判断第二路径是否有问题
			$path2 = splitpath($_POST['downpath2_1'],$_POST['downpath2_2'],$_POST['v_downterm2']);
			if($path2){
				$_POST['v_downpath2'] = $path2;
			}
			//判断第三路径是否有问题
			$path3 = splitpath($_POST['downpath3_1'],$_POST['downpath3_2'],$_POST['v_downterm3']);
			if($path3){
				$_POST['v_downpath3'] = $path3;
			}
			$m = M('movie');
			$_POST['v_addtime'] = date('Y-m-d');
			$res = $m->where('v_id='.$_POST['v_id'])->save($_POST);
			if($res){
				$this->success('修改成功');
				$_SESSION['now_movieimg'] = null;
			}else{
				$this->error('修改失败');
			}
		}else{
			$this->error('请以正确的方式提交');
		}
	}
	//电影删除
	public function movie_delete(){
		if(empty($_GET['v_id'])){
			$this->error('文章ID没有传递');
		}else{
			$id = $_GET['v_id'];
			$movie = M('movie');
			$res = $movie->where('v_id='.$id)->delete();
			if($res){
				$this->success('删除成功');
			}else{
			$this->error('删除失败');
			}	
		}
	}
}